package cn.wizzer.app.task.commons.ext.quartz.job;

import cn.wizzer.app.crown.modules.services.CrownConfigService;
import cn.wizzer.app.crown.modules.services.pl.bk.BKP3ResolverService;
import cn.wizzer.app.crown.modules.models.CrownConfig;
import cn.wizzer.app.crown.modules.services.pl.ft.P3ResolverService;
import cn.wizzer.framework.util.ThreadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;


/**
 * P3综合过关
 */
@IocBean
public class CrownJobMatchP3 implements Job {
    @Inject
    @Reference(retries = 3, timeout = 500000)
    private P3ResolverService ftP3;

    @Inject
    @Reference(retries = 3, timeout = 500000)
    private BKP3ResolverService bkP3;

    @Inject
    @Reference(retries = 0, timeout = 500000)
    private CrownConfigService crownConfigService;

    private static final Log log = Logs.get();

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 获取一个爬虫账号 线程安全
        CrownConfig crownConfig = crownConfigService.syncOne("P3综合过关任务");
        if (null != crownConfig) {
            log.info("准备开始任务:" + crownConfig);
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    crawler(crownConfig, jobExecutionContext);
                }
            });
        }

    }

    private void crawler(CrownConfig crownConfig, JobExecutionContext jobExecutionContext) {
        try {
            // 足球综合
            ftP3.p3(crownConfig);
            // 篮球综合
            bkP3.p3(crownConfig);
        } catch (Exception e) {
            log.error("综合定时任务异常" + e.getMessage());
        } finally {
            // 获取账号，判断是否为空，以防账号已经被清除
            crownConfigService.release(crownConfig);
        }
    }
}
